{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Transformer with non-trivial phase shift and tap ratio\n",
    "\n",
    "This example is a copy of pandapower's minimal example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pypsa\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network = pypsa.Network()\n",
    "\n",
    "network.add(\"Bus\", \"MV bus\", v_nom=20, v_mag_pu_set=1.02)\n",
    "network.add(\"Bus\", \"LV1 bus\", v_nom=0.4)\n",
    "network.add(\"Bus\", \"LV2 bus\", v_nom=0.4)\n",
    "\n",
    "network.add(\n",
    "    \"Transformer\",\n",
    "    \"MV-LV trafo\",\n",
    "    type=\"0.4 MVA 20/0.4 kV\",\n",
    "    bus0=\"MV bus\",\n",
    "    bus1=\"LV1 bus\",\n",
    ")\n",
    "network.add(\n",
    "    \"Line\", \"LV cable\", type=\"NAYY 4x50 SE\", bus0=\"LV1 bus\", bus1=\"LV2 bus\", length=0.1\n",
    ")\n",
    "network.add(\n",
    "    \"Generator\", \"External Grid\", bus=\"MV bus\", control=\"Slack\", marginal_cost=10\n",
    ")\n",
    "network.add(\"Load\", \"LV load\", bus=\"LV2 bus\", p_set=0.1, q_set=0.05)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def run_pf():\n",
    "    network.lpf()\n",
    "    network.pf(use_seed=True)\n",
    "    return pd.DataFrame(\n",
    "        {\n",
    "            \"Voltage Angles\": network.buses_t.v_ang.loc[\"now\"] * 180.0 / np.pi,\n",
    "            \"Volate Magnitude\": network.buses_t.v_mag_pu.loc[\"now\"],\n",
    "        }\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "run_pf()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network.transformers.tap_position = 2\n",
    "run_pf()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network.transformers.tap_position = -2\n",
    "run_pf()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now play with tap changer on LV side"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_trafo_lv_tap = network.transformer_types.loc[[\"0.4 MVA 20/0.4 kV\"]]\n",
    "new_trafo_lv_tap.index = [\"New trafo\"]\n",
    "new_trafo_lv_tap.tap_side = 1\n",
    "new_trafo_lv_tap.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network.transformer_types = network.transformer_types.append(new_trafo_lv_tap)\n",
    "network.transformers.type = \"New trafo\"\n",
    "network.transformers.tap_position = 2\n",
    "run_pf()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network.transformers.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network.transformers.tap_position = -2\n",
    "run_pf()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now make sure that the phase shift is also there in the LOPF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network.generators.p_nom = 1.0\n",
    "network.lines.s_nom = 1.0\n",
    "network.optimize()\n",
    "pd.DataFrame(\n",
    "    {\n",
    "        \"Voltage Angles\": network.buses_t.v_ang.loc[\"now\"] * 180.0 / np.pi,\n",
    "        \"Volate Magnitude\": network.buses_t.v_mag_pu.loc[\"now\"],\n",
    "    }\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "kernelspec": {
   "display_name": "",
   "language": "python",
   "name": ""
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
